/*
 * 
 * JPress版微信小程序
 * 
 */

var util = require('../../utils/util.js');
var jpress = require('../../utils/jpress.js');
var auth = require('../../utils/auth.js');

var app = getApp();
Page({
  data: {    
    prices: [
      6, 8, 18, 66, 88,188
    ],
    openId:'',
    articleId:'',
    total_fee:'',
    template_id: jpress.config.replayTemplateId,
    flag:'1'
  },

  /**
   * 进入页面
   */
  onLoad: function (options) { 
    var that=this;
    var openId = options.openId;
    var articleId = options.articleId;
    var flag = options.flag;
    that.setData({
        openId: openId,
        articleId: articleId,
        flag:flag
    });
  },
  cancel:function(){
      wx.navigateBack({
          delta: 1
      })
  },


  /**
   * 选中鼓励金额
   */
  selectItem: function (event) {
      var totalFee = event.currentTarget.dataset.item;
      var that = this;
      jpress.toUnifiedOrder({
          openId: that.data.openId,
          articleId: that.data.articleId,
          totalFee: totalFee
      }).then(response => {
          if (response.data) {
              var temp = response.data;
              wx.requestPayment({
                  timeStamp: response.data.timeStamp,
                  nonceStr: response.data.nonceStr,
                  package: response.data.package,
                  signType: 'MD5',
                  paySign: response.data.paySign,
                  success: function (res) {
                      var url = Api.updatePraiseUrl();
                      var data ={
                          openId: that.data.openId,
                          postid: that.data.postid,
                          orderid: response.data.nonceStr,
                          money: totalFee
                      }
                      var form_id = response.data.package;
                      form_id = form_id.substring(10);
                      var updatePraiseRequest = wxRequest.postRequest(url, data); //更新鼓励数据
                      updatePraiseRequest.then(response => {
                          console.log(response.data.message);
                      }).then(res => {
                          wx.showToast({
                              title: '谢谢鼓励！',
                              duration: 2000,
                              success: function () {
                                  data = {
                                      openId: that.data.openId,
                                      postid: that.data.postid,
                                      template_id: that.data.template_id,
                                      form_id: form_id,
                                      total_fee: totalFee,
                                      flag: that.data.flag,
                                      fromUser: "None"
                                  };
                                  url = Api.sendMessagesUrl();
                                  var sendMessageRequest = wxRequest.postRequest(url, data);
                                  sendMessageRequest.then(response => {
                                      if (response.data.status == '200') {
                                          console.log(response.data.message);
                                          wx.navigateBack({
                                              delta: 1
                                          })
                                      }else {
                                          console.log(response.data.message);
                                      }
                                  });
                              }
                          });
                      })
                  },
                  fail: function (res) {
                      wx.showToast({
                          title: res.errMsg,
                          icon: 'success'
                      });
                  },
                  complete: function (res) {
                      if (res.errMsg =='requestPayment:fail cancel'){
                          wx.showToast({
                              title: '取消鼓励',
                              icon: 'success'
                          });
                      }
                  }
              });
          }else {
              console.log(response.data.message);
          }
      })
  }
})
